package br.com.petshow.controller;

import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import br.com.petshow.app.Aplicacao;
import br.com.petshow.dao.ServicosDao;
import br.com.petshow.model.Servicos;
import br.com.petshow.utils.SqlField;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class ServicosController implements Initializable {

	private ServicosDao dao = null;

	@FXML
	private TextField tfCodigo;

	@FXML
	private TextField tfNome;

	@FXML
	private TextField tfFiltro;

	@FXML
	private Button btEdit;

	@FXML
	private TableView<Servicos> tvConsulta;

	@FXML
	private TableColumn clNome;

	@FXML
	private TableColumn clCodigo;
	
	@FXML
	private TableColumn clValor;

	@FXML
	private Button btSave;

	@FXML
	private Button btClear;

	@FXML
	private Button btDelete;

	@FXML
	private Button btExit;
	
	@FXML
    private TextField tfValor;

	@FXML
	void btSaveAction(ActionEvent event) {
		try {
			Servicos item = new Servicos();

			if (!tfCodigo.getText().trim().equals("")) {
				item.setId(Integer.parseInt(tfCodigo.getText()));
			}

			item.setNome(tfNome.getText());
			item.setPreco(Double.parseDouble(tfValor.getText()));

			dao.insertOrUpdate(item);
			btClearAction(null);
			carregarTabela(null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@FXML
	void btClearAction(ActionEvent event) {
		tfCodigo.setText("");
		tfNome.setText("");
		tfFiltro.setText("");
		tfValor.setText("");

		tfCodigo.requestFocus();
	}

	@FXML
	void btExitAction(ActionEvent event) {
		Aplicacao.loadUrl(Aplicacao.URL_MENU);
	}

	@FXML
	void btEditAction(ActionEvent event) {
		Servicos item = tvConsulta.getSelectionModel().getSelectedItem();

		tfCodigo.setText(String.valueOf(item.getId()));
		tfNome.setText(item.getNome());
		tfValor.setText(String.valueOf(item.getPreco()));
	}

	@FXML
	void btDeleteAction(ActionEvent event) {
		Servicos item = tvConsulta.getSelectionModel().getSelectedItem();
		dao.delete(item);
		carregarTabela(null);
	}

	@FXML
	void tfFiltroAction(ActionEvent event) {
		try {
			List<SqlField> filters = new ArrayList<SqlField>();
			SqlField filter = new SqlField("nome", "%" + tfFiltro.getText() + "%");
			filters.add(filter);

			List<Servicos> lista = dao.getList(filters);
			
			carregarTabela(lista);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void carregarTabela(List<Servicos> listaFiltered) {
		try {
			List<Servicos> lista;

			if (listaFiltered != null) {
				lista = listaFiltered;
			} else {
				lista = dao.getList();
			}

			clCodigo.setCellValueFactory(new PropertyValueFactory("id"));
			clNome.setCellValueFactory(new PropertyValueFactory("nome"));
			clValor.setCellValueFactory(new PropertyValueFactory("preco"));

			tvConsulta.setItems(FXCollections.observableArrayList(lista));
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		dao = new ServicosDao();
		carregarTabela(null);
	}

}
